#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#Kiwi Bot
#Copyright (C) 2009 Mathieu "MathX" Xhonneux
#Modified by @nge1nCorsic4 in 2010
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import kiwi.config
import sqlite3
from os.path import isfile

class bd:
	# def __init__(self):
		# self.bdFile()
	def bdFile(self):
		BDfile = kiwi.getPath(kiwi.config.configuration().kiwi.adminfile)
		if not isfile(BDfile):
			f = file(BDfile,'w')
			f.close()
		# print '1'
		# print BDfile
		return BDfile
		
		
class bd_client:
	def __init__(self):
		# print '2', bd()
		self.BDfile = bd().bdFile()
		self.createTables()
	def BDconnect(self):
		conn = sqlite3.connect(self.BDfile)
		conn.row_factory = sqlite3.Row
		return conn
	def createTables(self):
		connect = self.BDconnect()
		c = connect.cursor()
		c.execute('''create table if not exists client(name VARCHAR, guid VARCHAR, rights INTEGER, ip VARCHAR, live VARCHAR, dns VARCHAR)''')
		connect.commit()
		c.close()
	def writeTable(self,client):
		BDlist = ('name','guid','rights','ip','live','dns')
		connect = self.BDconnect()
		c = connect.cursor()
		result = []
		for a in BDlist:
			result.append(getattr(client,a))
		insert = "INSERT INTO client VALUES (?,?,?,?,?,?)"
		c.execute(insert, result)
		connect.commit()
		c.close()
	def updateTable(self,client):
		BDlist = ('name','rights','ip','live')
		connect = self.BDconnect()
		c = connect.cursor()
		for a in BDlist:
			insert = "update client set %s='%s' where guid='%s'"%(a,getattr(client,a),client.guid)
			c.execute(insert)
		connect.commit()
		c.close()
	def getTableAll(self):
		connect = self.BDconnect()
		cur = connect.cursor()
		select = "SELECT * FROM client"
		cur.execute(select)
		result = cur.fetchall()
		# print result
		return result
	def getTableByGuid(self, guid):
		BDlist = ('name','guid','rights','ip','live','dns')
		connect = self.BDconnect()
		c = connect.cursor()
		select = "SELECT * FROM client where guid='%s'"%guid
		c.execute(select)
		list = {}
		for user in c:
			for a in BDlist:
				list[a] = user[a]
		c.close()
		# print list
		return list
class bd_ban:
	def __init__(self):
		self.BDfile = bd().bdFile()
		self.createTables()
		print 'ban bd pluger'
	def BDconnect(self):
		conn = sqlite3.connect(self.BDfile)
		conn.row_factory = sqlite3.Row
		return conn
	def createTables(self):
		connect = self.BDconnect()
		c = connect.cursor()
		c.execute('''create table if not exists banlist(id INTEGER PRIMARY KEY, name VARCHAR, guid VARCHAR, timestamp INTEGER, timeban INTEGER, ip VARCHAR, admin VARCHAR)''')
		connect.commit()
		c.close()
	def writeTable(self,client):
		BDlist = ('id','name','guid','timestamp','timeban','ip','admin')
		connect = self.BDconnect()
		c = connect.cursor()
		result = []
		for a in BDlist:
			result.append(getattr(client,a))
		insert = "INSERT INTO banlist VALUES (?,?,?,?,?,?,?)"
		c.execute(insert, result)
		connect.commit()
		c.close()
	def updateTable(self,client):
		BDlist = ('id','name','guid','timestamp','timeban','ip','admin')
		connect = self.BDconnect()
		c = connect.cursor()
		for a in BDlist:
			insert = "update banlist set %s='%s' where guid='%s'"%(a,getattr(client,a),client.guid)
			c.execute(insert)
		connect.commit()
		c.close()
	def getTableAll(self):
		connect = self.BDconnect()
		cur = connect.cursor()
		select = "SELECT * FROM banlist"
		cur.execute(select)
		result = cur.fetchall()
		return result
	def getTableByGuid(self, guid):
		BDlist = ('id','name','guid','timestamp','timeban','ip','admin')
		connect = self.BDconnect()
		c = connect.cursor()
		select = "SELECT * FROM banlist where guid='%s'"%guid
		c.execute(select)
		list = {}
		for user in c:
			for a in BDlist:
				list[a] = user[a]
		c.close()
		# print list
		return list
